package com.ruoyi.web.controller.cat;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cat.domain.CatActivityTable;
import com.ruoyi.cat.service.ICatActivityTableService;
import com.ruoyi.common.utils.IpRegion;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.web.controller.api.IpAddressUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * h5活动Controller
 * 
 * @author ruoyi
 * @date 2024-12-26
 */
@RestController
@RequestMapping("/system/activityTable")
public class CatActivityTableController extends BaseController
{
    @Autowired
    private ICatActivityTableService catActivityTableService;

    /**
     * 查询h5活动列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatActivityTable catActivityTable)
    {
        startPage();
        List<CatActivityTable> list = catActivityTableService.selectCatActivityTableList(catActivityTable);
        return getDataTable(list);
    }

    /**
     * 下架文章任务
     */
    @PostMapping("offShelf")
    public AjaxResult offShelf(@RequestBody CatActivityTable catArticletaskTable)
    {
        return toAjax(catActivityTableService.updateCatActivity(catArticletaskTable));
    }

    /**
     * 导出h5活动列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:export')")
    @Log(title = "h5活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CatActivityTable catActivityTable)
    {
        List<CatActivityTable> list = catActivityTableService.selectCatActivityTableList(catActivityTable);
        ExcelUtil<CatActivityTable> util = new ExcelUtil<CatActivityTable>(CatActivityTable.class);
        util.exportExcel(response, list, "h5活动数据");
    }

    /**
     * 获取h5活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:table:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(catActivityTableService.selectCatActivityTableById(id));
    }

    @GetMapping(value = "getDetails/{id}")
    public AjaxResult getDetails(@PathVariable("id") Long id)
    {
        return success(catActivityTableService.getDetails(id));
    }

    /**
     * 新增h5活动
     */
    @PreAuthorize("@ss.hasPermi('system:table:add')")
    @Log(title = "h5活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody CatActivityTable catActivityTable)
    {
       /* String ipAddress = IpUtils.getIpAddr(request);
        IpRegion currentArea = IpAddressUtil.parseIpLocation(ipAddress);
        if (currentArea != null) {
            catActivityTable.setCity(currentArea.getCity());
            catActivityTable.setProvince(currentArea.getProvince());
        }*/
        return toAjax(catActivityTableService.insertCatActivityTable(catActivityTable));
    }

    /**
     * 修改h5活动
     */
    @PreAuthorize("@ss.hasPermi('system:table:edit')")
    @Log(title = "h5活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatActivityTable catActivityTable)
    {
        return toAjax(catActivityTableService.updateCatActivityTable(catActivityTable));
    }

    /**
     * 删除h5活动
     */
    @PreAuthorize("@ss.hasPermi('system:table:remove')")
    @Log(title = "h5活动", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(catActivityTableService.deleteCatActivityTableByIds(ids));
    }
}
